/*******************************发送分页查询ajax请求******************************/
var currentPage = 1; //当前页
var pageSize = 5; //每页显示条数
var totalCount = 0; //总记录条数
var totalPage = 0; //总页数

function PageFind() {
    //1.获取输入框上的值
    var fileName = $('#name').val();
    var startTime = $('#startCtime').val();
    var endTime = $('#endCtime').val();
    var fileState = $('#stateId').val();
    //2.发送ajax
    $.ajax({
        url: "fileInfo/pageFind",
        type: "post",
        dataType: "JSON",
        data: {
            FileName: fileName,
            startTime: startTime,
            endTime: endTime,
            fileState:fileState,
            currentPage: currentPage,
            pageSize: pageSize
        },
        success: function(pageBean) {
            //设置参数
            totalCount = pageBean.totalCount;
            totalPage = parseInt(pageBean.totalPage);
            //数据插入函数
            showTableData(pageBean.list);
        }
    });
};
//发送ajax
PageFind();

function showTableData(dataList) {
    //1.设置当前页和总页数
    var pageNumberData = currentPage + '/' + totalPage;
    $('#pageNumber').html(pageNumberData);
    //2.设置表格数据
    //2.1清空表格
    $('#tableData').html('');
    //2.2遍历数据
    var trs = '';
    for (var i = 0; i < dataList.length; i++) {
        var dataDetail = dataList[i];
        //改日期
        var cDate = new Date(dataDetail.upCtime).format('yyyy-MM-dd hh:mm:ss');
        var splitStrs = dataDetail.savePath.split("\\");
        var downLoadFileName = splitStrs[splitStrs.length-1];
        var hrefStr = "fileInfo/download?filename="+downLoadFileName+"&path="+dataDetail.savePath;
        var tr = `
                <tr>
                    <td hidden="hidden">${dataDetail.fileId}</td>
                    <td>${dataDetail.fileName}</td>
                    <td>${dataDetail.fileSize}</td>
                    <td>${cDate}</td>
                    <td>${dataDetail.typeName}</td>
                    <td>${dataDetail.fileScore}</td>
                    <td>${downLoadFileName}</td>
                    <td hidden="hidden">${dataDetail.savePath}</td>
                    <td>${dataDetail.userName}</td>
                `;
        var tr2=null;
        if(dataDetail.fileState==0){
            //待审核
            tr2=`   <td>待审核</td>
                    <td>
                        <a href=${hrefStr}>下载文件</a>
                        <button class=" btn btn-mini" type="button" id="pass">通过审核</button>
                        <button class=" btn btn-mini" type="button" id="noPass">不通过</button>
                    </td>
                </tr>`;
        }else if(dataDetail.fileState==1){
           //审核通过
            tr2=`   <td>审核通过</td>
                    <td>
                        <a href=${hrefStr}>下载文件</a>
                    </td>
                </tr>`;
        }else if(dataDetail.fileState==2){
            //审核不通过
            tr2=`   <td>审核不通过</td>
                    <td>
                        <a href=${hrefStr}>下载文件</a>
                    </td>
                </tr>`;
        }else if(dataDetail.fileState==3){
            //删除
            tr2=`   <td>删除</td>
                    <td>
                        <a href=${hrefStr}>下载文件</a>
                    </td>
                </tr>`;
        }
        //添加数据
        tr+=tr2;
        trs += tr;
    }
    //2.3将数据加入
    $('#tableData').html(trs);
    //3.审核通过事件
    $('[id = pass]').click(function() {
        var fileId = $(this).parent().parent().find('td').eq(0).html();
        $.ajax({
            url: "fileInfo/check",
            type: "post",
            dataType: "JSON",
            data: {
                fileStateId:1,
                fileId:fileId
            },
            success: function(jsonDto) {
                layer.alert(jsonDto.message);
                PageFind();
            }
        });
    });
    //4.审核通过事件
    $('[id = noPass]').click(function() {
        var fileId = $(this).parent().parent().find('td').eq(0).html();
        $.ajax({
            url: "fileInfo/check",
            type: "post",
            dataType: "JSON",
            data: {
                fileStateId:2,
                fileId:fileId
            },
            success: function(jsonDto) {
                layer.alert(jsonDto.message);
                PageFind();
            }
        });
    });
};
//上一页 下一页按钮 搜索 重置 触发事件
$('#prePageBtn').click(function() {
    currentPage -= 1;
    if (currentPage < 1) {
        currentPage = 1;
        return;
    }
    PageFind();
});
$('#nextPageBtn').click(function() {
    currentPage += 1;
    if (currentPage > totalPage) {
        currentPage = totalPage;
        return;
    }
    PageFind();
});
$('#searchBtn').click(function() {
    PageFind();
});
$('#reSetBtn').click(function() {
    $('#name').val('');
    $('#startCtime').val('');
    $('#endCtime').val('');
    $('#stateId').val(0);
});